#pragma once
#include <iostream>
//#include <string.h>

using namespace std;

namespace dr
{
	class string
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;
		string(const char* str = "")
			:_size(strlen(str))
		{
			_str = new char[_size + 1];
			_capacity = _size;
			strcpy(_str, str);
		}

		template <class InputIterator>
		string(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				push_back(*first);
				first++;
			}
		}

		/*string(const string& str)
			:_size(str._size)
		{
			_str = new char[_size + 1];
			_capacity = _size;
			strcpy(_str, str._str);
		}*/

		//string(const string& str)
		//	:_size(str._size)
		//{
		//	_str = new char[str._capacity + 1];
		//	_capacity = str._capacity;
		//	_size = str._size;
		//	memcpy(str._str, _str, str._capacity + 1);
		//}

		//string(const string& str)
		//{
		//	string tmp;
		//	tmp = str;
		//	this->swap(tmp);
		//}

		string(const string& str)
		{
			string tmp(str._str);
			swap(tmp);
		}


		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = 0;
			_capacity = 0;
		}

		string& operator= (const char* s);
		//string& operator= (const string& str);
		string& operator= (string str);
		void clear();
		const char* c_str() const;
		size_t size() const;
		size_t capacity() const;
		iterator begin();
		const_iterator begin() const;
		iterator end();
		const_iterator end() const;
		void reserve(size_t n = 0);
		char& operator[] (size_t pos);
		const char& operator[] (size_t pos) const;
		void push_back(char c);
		string& operator+= (char c);
		string& operator+= (const char* s);
		string& append(const char* s);
		void insert(size_t pos, const char* s);
		string& erase(size_t pos = 0, size_t len = npos);
		size_t find(char c, size_t pos = 0);
		size_t find(const char* s, size_t pos = 0) const;
		string substr(size_t pos = 0, size_t len = npos) const;
		void swap(string& s);

	private:
		char* _str = nullptr;
		size_t _size = 0;
		size_t _capacity = 0;
	public:
		static const size_t npos;
	};

	void swap(string& x, string& y);
	std::istream& operator>> (std::istream& is, string& str);
	std::ostream& operator<< (std::ostream& os, const string& str);

}